<?php
class model_message extends framework_base_model {

	private static $img_table = 'weixin_image_message';
	private static $keyword_table = 'weixin_keywords_message';
	
	/**
	 * 通过关键字获取消息
	 * @param int $category_id
	 * @return array $articles
	 */
	public static function get_msg_by_keyword($keywords) {
		$msg = array();
		$sql = "select message from ".self::$keyword_table." where keywords like '%".$keywords."%' and delete_flag = 0 order by id desc limit 0,1";
		try {
			$st = self::DB()->dbh->prepare($sql);
			$st->execute();
			$msg = $st->fetchColumn();
			
		} catch (Exception $e) {
			framework_static_function::write_log($e->getMessage(),'weixin_sql');
		}
		return $msg;
	}
	/**
	 * @info 添加图片消息
	 * @author jackie yang
	 * @datetime 20140727
	 * @param  $data  array
	 */
	public static function add_img_msg($data){
		try{
			$sql = "INSERT INTO ".self::$img_table." SET ";
			$binds = array();
			foreach ($data as $k => $v) {
				$sql .= '`' . $k . '`=:' . $k . ',';
				$binds[':' . $k] = $v;
			}
			$sql .= 'raw_add_time=NOW(),raw_update_time=now()';
			$arr=self::DB() -> dbh -> prepare($sql);
			$rel = $arr->execute($binds);
			if($rel == TRUE) {
				return self::DB()->dbh->lastInsertId();
			}
		}catch(Exception $e){
			framework_static_function::write_log("model_message/add_img_msg:".$e->getMessage(),'weixin_sql');
			return 0;
		}
		return 0;
	}
	/**
	 * 
	 * 根据订单号获取合同内容
	 * @param string $agreement_no 合同编号
	 */
	public static function get_order_info($agreement_no) {
		$sql ="select * from weixin_order where agreement_no=:agreement_no and delete_flag=0 order by raw_update_time desc limit 1";
		try {
			$st = self::DB()->dbh->prepare($sql);
			$binds[':agreement_no'] = $agreement_no;
			$st->execute($binds);
			$data = $st->fetch();			
		} catch (Exception $e) {
			$data = array();
			framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
		}
		return $data;
	}
}
